<?php

//esta clase podena cargar os administradores de organizacion e de empresa
class ProjectsAdminList extends ProjectsList
{

    protected $form_title;
    protected $form_description;
    protected $form_privacy;

    function setFormData( $title, $description, $privacy )
    {
        $this->form_title = $title;
        $this->form_description = $description;

        if ( empty( $_POST[ 'title' ] ) ) $this->errors[ ] = 'title';

        if ( empty( $_POST[ 'description' ] ) )
                $this->errors[ ] = 'description';

        if ( !in_array( $privacy, array( 'PUBLICO', 'PRIVADO' ) ) )
                $this->errors[ ] = '`privacy';
    }

    function handlePOST( Event $e )
    {
        if ( $e->tag == 'crear' ) {
            $this->_setFormaData( $_POST[ 'title' ], $_POST[ 'description' ],
                    $_POST[ 'privacy' ] );
            $e->cancelBubble();
        } else if ( $e->tag == 'eliminar' ) $e->cancelBubble();
    }

    function doPOST( Event $e )
    {
        if ( $e->tag == 'crear' ) {
            $this->crear();
        }
    }

    function crear()
    {
        //nombre imagen
        //tengo que establecer
        $sql = 'INSERT INTO milestones (resource, title, description, enterprise ) VALUES (\'R_PRO_' . ($_POST[ 'privacy' ] == 'PUBLICO' ? 'PUB' : 'PRV') . '\',' . MyLink::quote( $_POST[ 'title' ] ) . ',' . MyLink::quote( $_POST[ 'description' ] ) . ',' . $_SESSION[ 'enterprise' ] . ')';
        MyLink::$default->execute( $sql );

        $id = MyLink::$default->insert_id;

        $sql = 'UPDATE milestones SET project=' . $id . ',parent_milestone=' . $id . ' WHERE id=' . $id;
        MyLink::$default->execute( $sql );


        Security::addRolEdges( array( 'R_PRO_PUB' => array( $id ) ),
                'R_PRO_PRV', $id );
        Security::addRolEdges( array( 'R_MIL_PUB' => array( $id ) ),
                'R_MIL_PRV', $id );

        Security::addPrivilege( 'ADM_PRO', $id, 'R_USR_ENT',
                $_SESSION[ 'enterprise' ], 'ProjectUserNot' );
        Security::addPrivilege( 'ADM_PRO', $id, 'R_USR_PRO', $id, 'ProjectUser' );
        Security::addPrivilege( 'ADM_PRO', $id, 'R_ADM_PRO', $id,
                'ProjectUserAdminLight' );
        Security::addPrivilege( 'USR_ENT', $_SESSION[ 'enterprise' ],
                'R_ADM_PRO', $id, 'ProjectUserAdmin' );


        Security::addPrivilege( 'USR_ENT', $_SESSION[ 'enterprise' ],
                'R_PRO_PUB', $id, 'ProjectFull' );
        Security::addPrivilege( 'USR_PRO', $id, 'R_PRO_PRV', $id, 'ProjectFull' );
        Security::addPrivilege( 'ADM_PRO', $id, 'R_PRO_PRV', $id, 'ProjectAdmin' );
    }

}

?>
